﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;
using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using CaseManagerLibrary.Licence;
using PSDLib.Protocols;

namespace PSDManager.SystemSettings
{
    public partial class DeviceSettingPanel : UserControl, ISettingPanel
    {
        public ManagerHelper ManagerHelper { get; set; }

        private dl_device_default_config DefaultConfig = null;
        public DeviceSettingPanel()
        {
            InitializeComponent();
        }

        public void ApplySettings()
        {
            SaveSettings();
        }

        public void LoadSettings()
        {
            var result_psd = HttpHelper.Get<HResult<List<dl_download_device>>>(AppSettings.Setting.API.GetDownloadDeviceList);

            if (result_psd.success && result_psd.data != null)
            {
                this.lvwPSD.Items.Clear();

                foreach (var psd in result_psd.data)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = (this.lvwPSD.Items.Count + 1).ToString();
                    item.SubItems.Add(psd.device_name);
                    item.SubItems.Add(psd.device_version);
                    item.SubItems.Add(psd.device_ip);
                    item.SubItems.Add(psd.device_port.ToString());
                    var licence = LicenceManager.GetLicenceContent(psd.device_key, psd.id);
                    var code = LicenceManager.Check(licence, psd.id, LicenceDaysFrom.SysTime);

                    if (code == LicenceCheckCode.Success)
                    {
                        item.SubItems.Add("已注册");
                    }
                    else if (code == LicenceCheckCode.Expried)
                    {
                        item.SubItems.Add("授权已过期");
                    }
                    else
                    {
                        item.SubItems.Add("未注册");
                    }
                    item.SubItems.Add(psd.id);

                    item.Tag = psd;

                    this.lvwPSD.Items.Add(item);
                }
            }
        }

        public void SaveSettings()
        {
        }

        private void btnEditPSD_Click(object sender, EventArgs e)
        {
            if (this.lvwPSD.SelectedItems.Count > 0)
            {
                var psd = this.lvwPSD.SelectedItems[0].Tag as dl_download_device;
                FrmPSDDevice frm = new FrmPSDDevice();
                frm.StartPosition = FormStartPosition.CenterParent;
                //frm.SetServer(ManagerHelper.DownloadServerList);
                frm.SetPSD(psd);

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.RegistDownloadDevice, JsonConvert.SerializeObject(psd));

                    if (result.success)
                    {
                        var item = this.lvwPSD.SelectedItems[0];

                        item.Text = (this.lvwPSD.Items.Count + 1).ToString();
                        item.SubItems[1].Text = psd.device_name;
                        item.SubItems[2].Text = psd.device_version;
                        item.SubItems[3].Text = psd.device_ip;
                        item.SubItems[4].Text = psd.device_port.ToString();
                        if (psd.state == 0)
                        {
                            item.SubItems[5].Text = "未注册";
                        }
                        else if (psd.state == 1)
                        {
                            item.SubItems[5].Text = "已注册";
                        }
                        else if (psd.state == 2)
                        {
                            item.SubItems[5].Text = "授权过期";
                        }
                        item.SubItems[6].Text = psd.id;

                        ManagerHelper.UpdatePSDSetting(psd.id);

                    }
                }
                else
                {
                    LoadSettings();
                }
            }
        }

        private void btnDelPSD_Click(object sender, EventArgs e)
        {
            if (this.lvwPSD.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("是否确定删除该设备", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.DeleteDownloadDevice,
                        new Dictionary<string, string> {
                        { "deviceId", (this.lvwPSD.SelectedItems[0].Tag as dl_download_device).id }
                        });

                    if (result.success)
                    {
                        lvwPSD.SelectedItems[0].Remove();
                    }
                }
            }
        }
    }
}
